package tjs.study.mini.spingmvcmini.config;

import tjs.study.mini.spingmvcmini.config.servlet.DispatcherServlet;
import tjs.study.mini.spingmvcmini.config.tomact.WebApplicationInitializer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class MyWebAppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {
        System.out.println("调用自定义MyWebAppInitializer.onStartup....");
        // 1.启动SpringMVC 容器 类注入到Spring中
        AnnotationConfigWebApplicationContext app = new AnnotationConfigWebApplicationContext();// 启动SpringMVC Web
        // 2.注入springmvc 的配置文件
        app.register(MyWebMvcConfig.class);
        // 3. 将DispatcherServlet 注入到 serlvet容器中
        ServletRegistration.Dynamic dynamic =
                servletContext.addServlet("dispatcher", new DispatcherServlet(app));
        // 4.填写url路径映射
        //dynamic.addMapping("/mini/*");
        dynamic.addMapping("/");
        dynamic.setLoadOnStartup(1);// 优先级最高表示 最早被加载
    }
}
